package com.datou.controller;

import com.datou.datouspringmvc.annotation.*;
import com.datou.entity.Monster;
import com.datou.service.MonsterService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class MonsterController {

    @AutoWired
    private MonsterService monsterService;

    @RequestMapping(value = "/monster/list")
    public void listMonster(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        StringBuilder content = new StringBuilder();
        content.append("<h1>妖怪列表</h1>");
        content.append("<table border='1px' width='500px' style='border-collapse:collapse' >");
        List<Monster> monsters = monsterService.listMonsters();
        for(Monster monster : monsters) {
            content.append("<tr><td>" + monster.getId() + "</td><td>" + monster.getName() +"</td><td>" + monster.getKill() +"</td><td>" + monster.getAge() + "</td></tr>");
        }
        content.append("</table>");
        try {
            response.getWriter().write(content.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/monster/find")
    public void findMonstersByName(HttpServletRequest request, HttpServletResponse response, String name) {
        response.setContentType("text/html;charset=utf-8");
        System.out.println("------接收到的参数name = " + name);
        StringBuilder content = new StringBuilder();
        content.append("<h1>妖怪列表</h1>");
        content.append("<table border='1px' width='500px' style='border-collapse:collapse' >");
        List<Monster> monsters = monsterService.findMonsterByName(name);
        for(Monster monster : monsters) {
            content.append("<tr><td>" + monster.getId() + "</td><td>" + monster.getName() +"</td><td>" + monster.getKill() +"</td><td>" + monster.getAge() + "</td></tr>");
        }
        content.append("</table>");
        try {
            response.getWriter().write(content.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 处理monster登录，返回请求转发/请求重定向
    @RequestMapping(value = "/monster/login")
    public String login(HttpServletRequest req, HttpServletResponse resp, @RequestParam(value = "name")String name) {
        System.out.println("输入得name = " + name);
        boolean login = monsterService.login(name);
        req.setAttribute("name", name);
        if (login) {
            // 登录成功
//            return "forward:/login_ok.jsp";
//            return "redirect:/login_ok.jsp";
            return "/login_ok.jsp";
        }
        // 登录失败
        return "forward:/login_error.jsp";
    }


    // 返回json格式数据
    @ResponseBody
    @RequestMapping(value = "/monster/list/json")
    public List<Monster> listMonsterByJson(HttpServletRequest request, HttpServletResponse response) {
        return monsterService.listMonsters();
    }

}
